//
//  menu.mm
//  crazy_checkers
//
//  Created by Dmitriy Tatarinov on 11/3/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "menu.h"
#import "game.h"
#import "game2p.h"
#import "vars.h"
//#import "HiScore.h"
#import "gameover.h"




void MenuStage::Load()
{
	// число побед
	blackVictoryCount = 0;
	whiteVictoryCount = 0;
	//стартовая линия
	blackStartLine = 0;
	whiteStartLine = 0;	
	
	//timer=0;
		
	//logo=xLoadImage("logo.png");
	logo=xLoadImage("avalon.png");
	main=xLoadImage("main4.png");
//	options=xLoadImage("options2.png");
//	img_sound=xLoadImage("SoundOff.png");
	//img_cred=xLoadImage("credits4.jpg");
//	img_color=xLoadAnimImage("color.png",320,80,0,3);
	img_score=xLoadImage("top3l.png");
	img_sel=xLoadImage("sel_game.png");
	img_ai=xLoadImage("ai.png");
	
	lastGameBlackCount=8;
	lastGameWhiteCount=8;


	if (first_run){
		timer =1;
		first_run=false;
	}else{
		timer=51;
	}
	
	if (mode <3) mode=0;
	pvp=0;
	diffic=1;
	
	file_cfg=xReadFile("file://config.cfg");
	if (file_cfg)
	{
		soundPresent= xReadInt(file_cfg)==1;
		g_color=xReadInt(file_cfg);
		diffic=xReadInt(file_cfg);
		xCloseFile(file_cfg);
	}
	else {
		soundPresent=true;
		g_color=1;	
	}
	
	// load fonts
	_gmainFont             = xLoadFont("mainfont");

	snd_pick=xLoadSound("pick.m4a");
	
	if (soundPresent==true){
		xSoundVolume(snd_pick, 1);
	}else{
		xSoundVolume(snd_pick, 0);
	}
	
	// set stage as active
	MakeActive();
	total_score1=0;
	total_score2=0;

}

void MenuStage::Update()
{
	if (timer<51){
		if (xCountTouches() > 0) timer=50;x_t=0;y_t=0;	
	}
	
	if (soundPresent==1){
		xSoundVolume(snd_pick, 1);
	}else{
		xSoundVolume(snd_pick, 0);
	}
		
	
	check_Touch();
	xUpdateWorld(1.0);
}

void MenuStage::Render()
{
	
//	xRenderWorld();
	
	if (timer>0 & timer<51) timer++;
	
	if (timer<50)	{
		xDrawImage(logo, 0, 0,0);
	}else{
				
		if (mode==0){
			xDrawImage(main, 0, 0, 0);
			timer=51;
		}
		
		if (mode==1){
			xDrawImage(options, 0, 0, 0);
			
			if (soundPresent!=true)
			{
				xDrawImage(img_sound, 0, 107, 2);
			}
			
			switch(diffic){
				case 2:				
					xDrawImage(img_sound, 0, 203, 1);
					break;
				case 3:
					xDrawImage(img_sound, 0, 202, 0);
					break;
			}
					
			switch(g_color){
				case 2:			
					xDrawImage(img_color,0, 299, 0);
					break;
				case 3:
					xDrawImage(img_color, 0, 299, 1);
					break;
				case 4:
					xDrawImage(img_color, 0, 299, 2);
					break;
			}
			
		}
			
		if (mode==2){
			xDrawImage(img_cred,0, 0, 0);
		
		}
		
		if (mode==3){
			xDrawImage(img_score,0, 0, 0);
			xSetFont(_gmainFont);
			int scores[SCORE_COUNT];
			char names[SCORE_COUNT][50];
			long f=xReadFile("file://score.dat");
			for(int i = 0; i < SCORE_COUNT; i++){
				strcpy(names[i], xReadLine(f));
				scores[i] = xReadInt(f);
			}
			xCloseFile(f);		

			for (int i = 0; i < SCORE_COUNT; i++){
				char buff[70];
				sprintf(buff, "%i. %s", i+1, names[i]);
				xText(35, 126+i*53, buff, false, false);
				sprintf(buff, "%i", scores[i]);
				xText(228, 126+i*53, buff, false, false);
			}
		}
		
		if (mode==4){
			xDrawImage(img_sel,0, 0, 0);
			if (pvp==0) xDrawImage(img_ai, 0, 283, 0);
		}
		
	}
	// draw text information
	/*xSetFont(_gmainFont);
	char buff[256];
	sprintf(buff, "FPS: %i \n %i \n %i", xFPSCounter(),x_t,y_t);
	xText(5, 5, buff, false, false);
	*/
    xFlip();	
}

void MenuStage::Unload()
{
	file_cfg=xWriteFile("file://config.cfg");
		xWriteInt(file_cfg, (soundPresent?1:0));
		xWriteInt(file_cfg, g_color);
		xWriteInt(file_cfg, diffic);
	xCloseFile(file_cfg);					
	file_cfg = NULL;
	xFreeImage(logo);
	xFreeImage(main);
//	xFreeImage(options);
//	xFreeImage(img_sound);
//	xFreeImage(img_cred);
//	xFreeImage(img_color);
	xFreeImage(img_score);
	xFreeImage(img_sel);
	xFreeImage(img_ai);
	xFreeFont(_gmainFont);
	xFreeSound(snd_pick);
	xFlushTouches();
	logo = main= options =img_sound = img_cred = img_color = img_score = img_sel = img_ai = _gmainFont = NULL;
	snd_pick=NULL;
}

void MenuStage::check_Touch()
{
	
	for(int i = 0; i < xCountTouches(); i++)
	{
		x_t=xTouchX(i);
		y_t=xTouchY(i);
		
		if(xTouchPhase(i) == 1 & timer==51)
		{			
			if (mode==0){
				if (x_t>50 & x_t<270 & y_t>110 & y_t<170){
					mode=4;
					x_t=0;
					y_t=0;	
					xPlaySound(snd_pick);
				}
		
									
				if (x_t>50 & x_t<270 & y_t>185 & y_t<245){ //options
				//if (x_t>50 & x_t<270 & y_t>260 & y_t<315){	
					xPlaySound(snd_pick);
					img_color=xLoadAnimImage("color.png",320,80,0,3);
					options=xLoadImage("options2.png");
					img_sound=xLoadAnimImage("easy2.png",320,75,0,3);
					mode=1;
					x_t=0;
					y_t=0;					
				//	xFlushTouches();
				}
				
				if (x_t>50 & x_t<270 & y_t>260 & y_t<315){ //hiscore
				//if (x_t>50 & x_t<270 & y_t>330 & y_t<383){	
					mode=3;
					x_t=0;
					y_t=0;					
					xPlaySound(snd_pick);
				}
				
				if (x_t>50 & x_t<270 & y_t>330 & y_t<383){	//credits	
					x_t=0;
					y_t=0;					
					xPlaySound(snd_pick);
					img_cred=xLoadImage("credits4.jpg");
					mode=2;
							
				}	
			
				if (x_t>50 & x_t<270 & y_t>410 & y_t<460){ //more games
				//	mode=2;
                    

					x_t=0;
					y_t=0;
					xPlaySound(snd_pick);
				}
			}
			
			if (mode==1){ //options
			
				if (x_t>50 & x_t<270 & y_t>410 & y_t<450){	
					xPlaySound(snd_pick);
					xFreeImage(options);
					xFreeImage(img_sound);
					mode=0;
				}	
				
				if (x_t>50 & x_t<270 & y_t>105 & y_t<160){	
					xPlaySound(snd_pick);
					soundPresent= !soundPresent;
				}	
				
				if (x_t>50 & x_t<270 & y_t>200 & y_t<255){	
					xPlaySound(snd_pick);
					diffic++;
					if (diffic>3) diffic=1;
					//1-nornal, 2-hard, 3-easy
				}
				
				
				if (x_t>50 & x_t<270 & y_t>300 & y_t<380){
					xPlaySound(snd_pick);
					g_color++;
					if (g_color>4) g_color=1;
				}
				
			}
			
			
			if (mode==2){ //credits
				
				if (x_t>50 & x_t<270 & y_t>410 & y_t<450){
					xPlaySound(snd_pick);
					xFreeImage(img_cred);
					mode=0;
				}
				
			}
			
			if (mode==3){ //score
				
				if (x_t>50 & x_t<270 & y_t>420 & y_t<475){
					xPlaySound(snd_pick);
					mode=0;

				}
				
			}
			
			if (mode==4){
					//xPlaySound(sound0);
				
				if (x_t>50 & x_t<270 & y_t>195 & y_t<250){	
				//if (x_t>50 & x_t<270 & y_t>120 & y_t<175){	
						quick_game=1;
						gameProhod=1;
						xPlaySound(snd_pick);
					if (pvp!=1){
						Stage* game = new GameStage();
						game->Load();
						numPlayers = 1;
						level_cnt=1;
						
				//		Stage* Gameover = new GameoverStage();
				//		Gameover->Load();
						
						return;
					}else{
						Stage* game = new GameStage2p();
						game->Load();
						numPlayers = 2;
						level_cnt=1;
						return;
					}
				}
				
				if (x_t>50 & x_t<270 & y_t>120 & y_t<175){	
				//if (x_t>50 & x_t<270 & y_t>195 & y_t<250){	
						quick_game=0;
						gameProhod=1;
						xPlaySound(snd_pick);
					if (pvp!=1){
						Stage* game = new GameStage();
						game->Load();
						numPlayers = 1;
						level_cnt=1;
						return;
					}else{
						Stage* game = new GameStage2p();
						game->Load();
						numPlayers = 2;
						level_cnt=1;
						return;
					}
				}			
				
				
				if (x_t>50 & x_t<270 & y_t>290 & y_t<365){	
					pvp=1-pvp;
					xPlaySound(snd_pick);
				}
								
				if (x_t>50 & x_t<270 & y_t>410 & y_t<450){	
					mode=0;
					xPlaySound(snd_pick);
				}
			}
		}
	}
		
}
